package com.unitymarvel.harmonyos.projects.findme.common;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.wifi.WifiDevice;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;
///////////////////////////////////////////////////
// 项目的详细实现过程：https://ke.qq.com/course/3677276?tuin=a22a65ce
// 关注微信公众号和B站，获取更多鸿蒙学习资源
// 微信公众号：极客起源
// B站：https://space.bilibili.com/477001733
//
// 作者:李宁（蒙娜丽宁）
//
// 版本: 1.0
// Copyright (C), 2001-2022, 李宁
// 程序被版权法保护
//
///////////////////////////////////////////////////
public class Tools {
    // 用于显示提示
    public static void showTip(Context context, String content) {
        Text text = new Text(context);
        text.setWidth(MATCH_CONTENT);
        text.setHeight(MATCH_CONTENT);
        text.setTextSize(50);
        text.setText(content);
        text.setPadding(30, 20, 30, 20);
        text.setMultipleLine(true);
        text.setTextColor(Color.WHITE);
        text.setTextAlignment(TextAlignment.CENTER);

        ShapeElement style = new ShapeElement();
        style.setShape(ShapeElement.RECTANGLE);
        style.setRgbColor(new RgbColor((0x666666FF)));
        style.setCornerRadius(15);
        text.setBackground(style);

        DirectionalLayout mainLayout = new DirectionalLayout(context);
        mainLayout.setWidth(MATCH_PARENT);
        mainLayout.setHeight(MATCH_CONTENT);

        mainLayout.setAlignment(LayoutAlignment.CENTER);

        mainLayout.addComponent(text);

        ToastDialog toastDialog = new ToastDialog(context);
        toastDialog.setSize(MATCH_PARENT, MATCH_CONTENT);
        toastDialog.setDuration(5000);
        toastDialog.setTransparent(true);

        toastDialog.setAlignment(LayoutAlignment.CENTER);
        toastDialog.setComponent(mainLayout);
        toastDialog.show();
    }
    // 获取字符的文字颜色和背景色
    // colors[0] = "#FA129B"
    // colors[1] = "#......"
    public static  String[] getTwoRandomHexColor() {
        int red1 = (int)(Math.random() * 128);
        int red2 = 255-red1;
        int green1 = (int)(Math.random() * 128);
        int green2 = 255-green1;
        int blue1 = (int)(Math.random() * 128);
        int blue2 = 255-blue1;

        String[] colors = new String[2];
        colors[0] = "#" + String.format("%02x", red1) + String.format("%02x", green1) + String.format("%02x", blue1);
        colors[1] = "#" + String.format("%02x", red2) + String.format("%02x", green2) + String.format("%02x", blue2);
        // 0xF10x430xA1
        colors[0] = colors[0].replace("0x", "");
        colors[1] = colors[1].replace("0x","");

        return colors;
    }
    public static String getLocalIP(Context context) {
        try {
            // 00000000 00000000 00000000 xxxxxxxx
            int ip = WifiDevice.getInstance(context).getIpInfo().get().getIpAddress();
            String ipStr = String.format("%d.%d.%d.%d", (ip & 0xff), (ip >> 8 & 0xff), (ip >> 16 & 0xff), (ip >> 24 & 0xff));
            return ipStr;

        } catch (Exception e) {

        }
        return "";
    }
}
